/**
 * 包含多个工具函数的模块
 */

/**
 * 用户主界面路由
 *      dashen： /dashen
 *      laoban： /laoban
 * 用户信息完善见面路由
 *      dashen： /dasheninfo
 *      loaban： /laobaninfo
 * 判断用户的类型/用户信息是否完善
 */
export function getRedirectTo(type, header) {
  let path;
  //  判断用户的类型
  if (type === "laoban") {
    path = "/laoban";
  } else if (type === "dashen") {
    path = "/dashen";
  }
  //  没有完善用户信息跳转到完善用户信息页面
  if (!header) {
    path += "info";
  }
  return path;
}


// Proxy代理 递归监听深层数据
export function deepProxy(obj, cb) {
  if (typeof obj === "object") {
    for (let key in obj) {
      if (typeof obj[key] === "object") {
        obj[key] = deepProxy(obj[key], cb);
      }
    }
  }
  return new Proxy(obj, {
    /**
     * @param {Object, Array} target 设置值的对象
     * @param {String} key 属性
     * @param {any} value 值
     * @param {Object} receiver this
     */
    set: function (target, key, value, receiver) {
        console.log("Proxy", target, key, value, receiver);
        if (typeof value === "object") {
            value = deepProxy(value, cb);
        }
        let cbType = target[key] === undefined ? "create" : "modify";
        //排除数组修改length回调
        if (!(Array.isArray(target) && key === "length")) {
            cb(cbType, { target, key, value });
        }
        Reflect.set(target, key, value, receiver);
        return true
    },
    deleteProperty(target, key) {
        cb("delete", { target, key });
        return Reflect.deleteProperty(target, key);
    },
  });
}
